package cn.licoy.wdog.core.controller.hospital2.pay;
/**
 * @Author Sunne
 * @Date 18-8-13上午10:21
 * @Param emp-hospital
 * 高山仰止,景行行止.虽不能至,心向往之。
 * ——————————————————————————女神保佑，永无bug——————————————————————
 **/

import cn.licoy.wdog.common.bean.ResponseResult;
import cn.licoy.wdog.common.util.pay.WXPay;
import cn.licoy.wdog.common.util.pay.WXPayConfig;
import cn.licoy.wdog.common.util.pay.WXPayConstants;
import cn.licoy.wdog.core.config.wx.MyConfig;
import cn.licoy.wdog.core.dto.hospital2.pay.CreateOrderDto;
import cn.licoy.wdog.core.dto.hospital2.pay.OrderQueryDto;
import cn.licoy.wdog.core.dto.hospital2.pay.PayDto;
import cn.licoy.wdog.core.entity.hospital2.Pay;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.service.ResponseMessage;

import java.util.HashMap;
import java.util.Map;

/**
 *                    .::::.
 *                  .::::::::.
 *                 :::::::::::  FUCK YOU
 *             ..:::::::::::'
 *           '::::::::::::'
 *             .::::::::::
 *        '::::::::::::::..
 *             ..::::::::::::.
 *           ``::::::::::::::::
 *            ::::``:::::::::'        .:::.
 *           ::::'   ':::::'       .::::::::.
 *         .::::'      ::::     .:::::::'::::.
 *        .:::'       :::::  .:::::::::' ':::::.
 *       .::'        :::::.:::::::::'      ':::::.
 *      .::'         ::::::::::::::'         ``::::.
 *  ...:::           ::::::::::::'              ``::.
 * ```` ':.          ':::::::::'                  ::::..
 *                    '.:::::'                    ':'````..
 */
@RestController
@RequestMapping(value = "/pay")
@Api(tags = {"第二期--支付接口"})
public class PayController {
    @PostMapping(value = {"/createOrder"})
    @ApiOperation(value = "订单生产")
    public ResponseResult<Pay> createOrder(@RequestBody @Validated @ApiParam("订单实体") CreateOrderDto createOrderDto){
        try {
            MyConfig config = new MyConfig();
            WXPay wxpay = new WXPay(config);
            Map<String, String> data = new HashMap<String, String>();
            data.put("body", createOrderDto.getBody());
            data.put("out_trade_no", "2016090910595900000012");
            data.put("device_info", "");
            data.put("fee_type", "CNY");
            data.put("total_fee", createOrderDto.getTotal_fee());
            data.put("spbill_create_ip", "123.12.12.123");
            data.put("notify_url", "http://www.example.com/wxpay/notify");//通知地址
            data.put("trade_type", "APP");// 此处指定为App支付
            data.put("product_id", "12");
            Map<String, String> resp = wxpay.unifiedOrder(data);

            System.out.println(resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    @PostMapping(value = {"/selectOrderByTid"})
    @ApiOperation(value = "通过订单号查询订单")
    public ResponseResult<Pay> orderQuery(@RequestBody @Validated @ApiParam("订单实体") OrderQueryDto orderQueryDto){
        MyConfig config = null;
        WXPay wxpay = null;
        try {
            config = new MyConfig();
            wxpay = new WXPay(config);
            Map<String, String> data = new HashMap<String, String>();
            data.put("out_trade_no",orderQueryDto.getOut_trade_no());
            Map<String, String> resp = wxpay.orderQuery(data);
            System.out.println(resp);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
    @PostMapping(value = {"/reverseOrderByTid"})
    @ApiOperation(value = "通过订单号撤销订单")
    public ResponseResult<Pay> orderReverse(@RequestBody @Validated @ApiParam("订单实体") OrderQueryDto orderQueryDto){
        MyConfig config = null;
        WXPay wxpay = null;
        try {
            config = new MyConfig();
            wxpay = new WXPay(config);
            Map<String, String> data = new HashMap<String, String>();
            data.put("out_trade_no",orderQueryDto.getOut_trade_no());
            Map<String, String> resp = wxpay.reverse(data);
            System.out.println(resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @PostMapping(value = {"/closeOrderByTid"})
    @ApiOperation(value = "通过订单号关闭订单")
    public ResponseResult<Pay> closeOrder(@RequestBody @Validated @ApiParam("订单实体") OrderQueryDto orderQueryDto){
        MyConfig config = null;
        WXPay wxpay = null;
        try {
            config = new MyConfig();
            wxpay = new WXPay(config);
            Map<String, String> data = new HashMap<String, String>();
            data.put("out_trade_no",orderQueryDto.getOut_trade_no());
            Map<String, String> resp = wxpay.closeOrder(data);
            System.out.println(resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    @PostMapping(value = {"/refundOrderByTid"})
    @ApiOperation(value = "通过订单号申请微信退款（微信付款未到账或者出现异常时使用）")
    public ResponseResult<Pay> refundOrder(@RequestBody @Validated @ApiParam("订单实体") OrderQueryDto orderQueryDto){
        MyConfig config = null;
        WXPay wxpay = null;
        try {
            config = new MyConfig();
            wxpay = new WXPay(config);
            Map<String, String> data = new HashMap<String, String>();
            data.put("out_trade_no",orderQueryDto.getOut_trade_no());
            Map<String, String> resp = wxpay.refund(data);
            System.out.println(resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    @PostMapping(value = {"/refundQueryById"})
    @ApiOperation(value = "退款查询")
    public ResponseResult<Pay> refundQueryById(@RequestBody @Validated @ApiParam("订单实体") OrderQueryDto orderQueryDto){
        MyConfig config = null;
        WXPay wxpay = null;
        try {
            config = new MyConfig();
            wxpay = new WXPay(config);
            Map<String, String> data = new HashMap<String, String>();
            data.put("out_trade_no",orderQueryDto.getOut_trade_no());
            Map<String, String> resp = wxpay.refundQuery(data);
            System.out.println(resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @PostMapping(value = {"/downloadBillById"})
    @ApiOperation(value = "通过订单下载对账单")
    public ResponseResult<Pay> downloadBillById(@RequestBody @Validated @ApiParam("订单实体") OrderQueryDto orderQueryDto){
        MyConfig config = null;
        WXPay wxpay = null;
        try {
            config = new MyConfig();
            wxpay = new WXPay(config);
            Map<String, String> data = new HashMap<String, String>();
            data.put("out_trade_no",orderQueryDto.getOut_trade_no());
            Map<String, String> resp = wxpay.downloadBill(data);
            System.out.println(resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    @PostMapping(value = {"/authCodeToOpenid"})
    @ApiOperation(value = "授权码查询")
    public ResponseResult<Pay> authCodeToOpenid(@RequestBody @Validated @ApiParam("订单实体") OrderQueryDto orderQueryDto) {
        MyConfig config = null;
        WXPay wxpay = null;
        try {
            config = new MyConfig();
            wxpay = new WXPay(config);
            Map<String, String> data = new HashMap<String, String>();
            data.put("out_trade_no", orderQueryDto.getOut_trade_no());
            Map<String, String> resp = wxpay.authCodeToOpenid(data);
            System.out.println(resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
